using System;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Runtime.Serialization;

namespace WcfService1.DTO
{
    [DataContract]
    public class PreSalesOrderDTO : INotifyPropertyChanged
    {
        private string _Address;
        private string _CustomerName;
        private String _Description;
        private int _No;
        private String _PSNo;
        private ObservableCollection<PreSalesOrderDetailDTO> _PreSalesOrderDetails;


        private DateTime? _SRDate;
        private Status _Status;
        private string _Telp;
        private Decimal _Total;
        private String _created_by;
        private DateTime? _created_date;
        private String _modified_by;
        private DateTime? _modified_date;
        private string _SoNo;
        private string _SiNo;
        private string _DONo;
        private WarehouseDTO _Warehouse;

        public PreSalesOrderDTO()
        {
            SRDate = DateTime.Now.Date;
        }

        [DataMember]
        public String PSNo
        {
            get { return _PSNo; }
            set
            {
                _PSNo = value;
                RaisePropertyChanged("PSNo");
            }
        }

        [DataMember]
        public DateTime? SRDate
        {
            get { return _SRDate; }
            set
            {
                _SRDate = value;
                RaisePropertyChanged("SRDate");
            }
        }


        [DataMember]
        public String Description
        {
            get { return _Description; }
            set
            {
                _Description = value;
                RaisePropertyChanged("Description");
            }
        }


        [DataMember]
        public String SoNo
        {
            get { return _SoNo; }
            set
            {
                _SoNo = value;
                RaisePropertyChanged("SoNo");
            }
        }

        [DataMember]
        public String SiNo
        {
            get { return _SiNo; }
            set
            {
                _SiNo = value;
                RaisePropertyChanged("SiNo");
            }
        }

        [DataMember]
        public String DONo
        {
            get { return _DONo; }
            set
            {
                _DONo = value;
                RaisePropertyChanged("DONo");
            }
        }

        [DataMember]
        public String created_by
        {
            get { return _created_by; }
            set
            {
                _created_by = value;
                RaisePropertyChanged("created_by");
            }
        }


        [DataMember]
        public DateTime? created_date
        {
            get { return _created_date; }
            set
            {
                _created_date = value;
                RaisePropertyChanged("created_date");
            }
        }

        [DataMember]
        public String modified_by
        {
            get { return _modified_by; }
            set
            {
                _modified_by = value;
                RaisePropertyChanged("modified_by");
            }
        }


        [DataMember]
        public DateTime? modified_date
        {
            get { return _modified_date; }
            set
            {
                _modified_date = value;
                RaisePropertyChanged("modified_date");
            }
        }

        [DataMember]
        public ObservableCollection<PreSalesOrderDetailDTO> PreSalesOrderDetails
        {
            get { return _PreSalesOrderDetails; }
            set
            {
                if (value == PreSalesOrderDetails) return;
                _PreSalesOrderDetails = value;
                RaisePropertyChanged("PreSalesOrderDetails");
                RefreshNumber();
            }
        }


        [DataMember]
        public Status Status
        {
            get { return _Status; }
            set
            {
                _Status = value;
                RaisePropertyChanged("Status");
            }
        }

        public int No
        {
            get { return _No; }
            set
            {
                _No = value;
                RaisePropertyChanged("No");
            }
        }

        public Decimal Total
        {
            get { return _Total; }
            set
            {
                _Total = value;
                RaisePropertyChanged("Total");
            }
        }

        [DataMember]
        public String CustomerName
        {
            get { return _CustomerName; }
            set
            {
                _CustomerName = value;
                RaisePropertyChanged("CustomerName");
            }
        }


        [DataMember]
        public String Address
        {
            get { return _Address; }
            set
            {
                _Address = value;
                RaisePropertyChanged("Address");
            }
        }


        [DataMember]
        public String Telp
        {
            get { return _Telp; }
            set
            {
                _Telp = value;
                RaisePropertyChanged("Telp");
            }
        }

        [DataMember]
        public WarehouseDTO Warehouse
        {
            get { return _Warehouse; }
            set
            {
                _Warehouse = value;
                RaisePropertyChanged("Warehouse");
            }
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        private void RefreshNumber()
        {
            PreSalesOrderDetails.CollectionChanged += PreSalesOrderDetails_CollectionChanged;
            int number = 0;
            foreach (PreSalesOrderDetailDTO DetailDto in PreSalesOrderDetails)
            {
                number += 1;
                DetailDto.No = number;
            }
        }

        private void PreSalesOrderDetails_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            int number = 0;
            foreach (PreSalesOrderDetailDTO DetailDto in PreSalesOrderDetails)
            {
                number += 1;
                DetailDto.No = number;
            }
        }

        protected void RaisePropertyChanged(string propertyname)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(propertyname));
        }
    }
}